package com.zeta.eduservice.controller;


import javax.servlet.http.HttpServletRequest;

import org.apache.xmlbeans.impl.regex.REUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.LoginVo;
import com.zeta.eduservice.entity.RegisterVo;
import com.zeta.eduservice.entity.UcenterMember;
import com.zeta.eduservice.entity.infoVo;
import com.zeta.eduservice.service.UcenterMemberService;
import com.zeta.exceptionhandler.GuliException;

import com.zeta.utils.JwtUtils;

import io.swagger.annotations.ApiOperation;
import lombok.experimental.PackagePrivate;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-08-09
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/ucenter_member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;


    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo member) {
        String token = memberService.login(member);

        return R.ok().data("token", token);
    }

    @PutMapping("")
    public R update(@RequestBody  UcenterMember entity) {
    	
  
    	memberService.updateById(entity);
    	return R.ok();
    }
    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo formItem){
        memberService.register(formItem);
        return R.ok();
    }
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        member.setPassword("");
        
        return R.ok().data("userInfo",member);
    }
    

	
	@GetMapping(value = "{day}")
	public R registerCount(@PathVariable String day){
	    Integer count = memberService.countRegisterByDay(day);
	    return R.ok().data("countRegister", count);
	}
	
	@PostMapping("/binding")
	public R binding(@RequestBody infoVo info) {
		memberService.binding(info);
		return R.ok();
	}
}

